package cn.study.dl;

import java.lang.management.ManagementFactory;

/**
 *@author 跟大佳一起学架构
 *
 *类说明：演示死锁的产生
 */
public class NormalDeadLock {

    private static Object No1 = new Object();//第一个锁
    private static Object No2 = new Object();//第二个锁

    //第一个拿锁的方法
    private static void thread1Do() throws InterruptedException {
        String threadName = Thread.currentThread().getName();
        synchronized (No1){
            System.out.println(threadName + " get No1");
            Thread.sleep(100);
            synchronized (No2){
                System.out.println(threadName + " get No2");
            }
        }

    }
    //第二个拿锁的方法
    private static void thread2Do() throws InterruptedException {
        String threadName = Thread.currentThread().getName();
        synchronized (No2){
            System.out.println(threadName + " get No1");
            Thread.sleep(100);
            synchronized (No1){
                System.out.println(threadName + " get No2");
            }
        }
    }
    //子线程1
    private static class Thread1 extends Thread{
        private String name;
        public Thread1(String name) {
            this.name = name;
        }
        @Override
        public void run() {
            Thread.currentThread().setName(name);
            try {
                thread1Do();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //主线程
        Thread.currentThread().setName("Thread1");
        Thread1 thread1 = new Thread1("Thread1");
//        System.out.println(ManagementFactory.getRuntimeMXBean().getName());
        thread1.start();
        thread2Do();
    }

}
